package com.wpl.fastec.latte.core.net.download;

import android.os.AsyncTask;

import com.wpl.fastec.latte.core.net.RestCreator;
import com.wpl.fastec.latte.core.net.callback.IError;
import com.wpl.fastec.latte.core.net.callback.IFailure;
import com.wpl.fastec.latte.core.net.callback.IRequest;
import com.wpl.fastec.latte.core.net.callback.ISuccess;

import java.util.WeakHashMap;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DownloadHandler {
    private final String URL;
    private static final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();
    private final IRequest REQUEST;
    private final ISuccess SUCCESS;
    private final IFailure FAILURE;
    private final IError ERROR;
    private final String DOWMLOAD_DIR;
    private final String EXTENSION;
    private final String NAME;

    public DownloadHandler(String url,
                           IRequest request,
                           ISuccess success,
                           IFailure failure,
                           IError error,
                           String downloadDir,
                           String extension,
                           String name) {
        this.URL = url;
        this.REQUEST = request;
        this.SUCCESS = success;
        this.FAILURE = failure;
        this.ERROR = error;
        this.DOWMLOAD_DIR = downloadDir;
        this.EXTENSION = extension;
        this.NAME = name;
    }

    public final void handlerDownload() {
        if (REQUEST != null){
            REQUEST.onRequestStart();
        }

        RestCreator.getRestService().download(URL,PARAMS)
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                       if(response.isSuccessful()) {
                           final ResponseBody body = response.body();
                           final SaveFileTask task = new SaveFileTask(REQUEST,SUCCESS);
                           task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,DOWMLOAD_DIR,
                                   EXTENSION,
                                   body,
                                   NAME);
//                        这里一定要判断文件是否下载完成
                           if (task.isCancelled()) {
                               if(REQUEST != null) {
                                   REQUEST.onRequestEnd();
                               }
                           }
                       } else {
                           if (ERROR != null) {
                               ERROR.onError(response.code(),response.message());
                           }
                       }

                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        if (FAILURE != null) {
                            FAILURE.onFailure();
                        }
                    }
                });
    }
}
